home *** CD-ROM | disk | FTP | other *** search
/ Greenhouse Effect Detection Expriment / NASA Greenhouse Effect Detection Expriment 1992 - Disc 2.iso / software / dos / cdf22pc / src / tools / cdf2skt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-04  |  32.4 KB  |  1,131 lines

  1. /******************************************************************************
  2. *
  3. *  NSSDC/CDF                            SkeletonTable.
  4. *
  5. *  Version 1.0, 4-Mar-92, Hughes STX
  6. *
  7. *  Modification history:
  8. *
  9. *   V1.0   4-Mar-92, J Love       Original version.
  10. *
  11. ******************************************************************************/
  12.  
  13. #include "cdfdist.h"
  14. #include "cdf2skt.h"
  15.  
  16. /******************************************************************************
  17. * Global variables.
  18. ******************************************************************************/
  19.  
  20. CDFid id;
  21. Boolean mLog;
  22. FILE *SKTfp;
  23. FILE *NRVfp;
  24. enum NRVlocENUM NRVloc;
  25.  
  26. /******************************************************************************
  27. * Online instructions.
  28. ******************************************************************************/
  29.  
  30. static char *instructions[] = {
  31. #if defined(vms)
  32. "Usage:         $ SKELETONTABLE [/SKELETON=<skeleton-path>]",
  33. "                               [/noNRV or /NRVfile or /NRVtable] [/[no]LOG]",
  34. "                               <cdf-path>",
  35. #endif
  36. #if defined(unix)
  37. "Usage:         % skeletontable [-skeleton <skeleton-path>]",
  38. "                               [-noNRV or -NRVfile or -NRVtable] [-[no]log]",
  39. "                               <cdf-path>",
  40. #endif
  41. #if defined(__MSDOS__)
  42. "Usage:         > skeletontable [-skeleton <skeleton-path>]",
  43. "                               [-noNRV or -NRVfile or -NRVtable] [-[no]log]",
  44. "                               <cdf-path>",
  45. #endif
  46. "",
  47. "Purpose:       SkeletonTable produces a skeleton table from a CDF.",
  48. "",
  49. "               A skeleton table is a text file which can be read",
  50. "               by the CDFskeleton program to build a skeleton CDF.",
  51. "",
  52. "Parameter(s):  <cdf-path>",
  53. "                  The pathname of the CDF to read.  Do not enter an",
  54. "                  extension.  (Required).",
  55. "",
  56. #if defined(vms)
  57. "Qualifier(s):  /SKELETON=<skeleton-path>",
  58. "                  <skeleton-path> is the pathname of the skeleton table file",
  59. "                  to be created.  Any valid pathname may be specified (do",
  60. "                  not enter an extension).  If not specified, the default",
  61. "                  skeleton table file name is <cdf-name>.skt in the default",
  62. "                  directory (where <cdf-name> is the name portion of",
  63. "                  <cdf-path>).  (Optional).",
  64. #endif
  65. #if defined(unix) | defined(__MSDOS__)
  66. "Qualifier(s):  -skeleton <skeleton-path>",
  67. "                  <skeleton-path> is the pathname of the skeleton table file",
  68. "                  to be created.  Any valid pathname may be specified (do",
  69. "                  not enter an extension).  If not specified, the default",
  70. "                  skeleton table file name is <cdf-name>.skt in the current",
  71. "                  directory (where <cdf-name> is the name portion of",
  72. "                  <cdf-path>).  (Optional).",
  73. #endif
  74. "",
  75. #if defined(vms)
  76. "               /noNRV",
  77. "               /NRVtable",
  78. "               /NRVfile",
  79. "                  No more than one of these qualifiers may be specified.  If",
  80. "                  none are specified, the default is /NRVtable.  (Optional).",
  81. "                    /noNRV......Ignore all non-record variant (NRV) data",
  82. "                    /NRVtable...Put NRV data values in the skeleton table",
  83. "                    /NRVfile....Put NRV data values in a separate file.  The",
  84. "                                name of this file will be <skeleton-path>.nrv",
  85. "                                if the /SKELETON qualifier was used.  It will",
  86. "                                be <cdf-name>.nrv in the default directory",
  87. "                                (where <cdf-name> is the name portion of",
  88. "                                <cdf-path>) if /SKELETON was not used.",
  89. #endif
  90. #if defined(unix) | defined(__MSDOS__)
  91. "               -noNRV",
  92. "               -NRVtable",
  93. "               -NRVfile",
  94. "                  No more than one of these qualifiers may be specified.  If",
  95. "                  none are specified, the default is -NRVtable.  (Optional).",
  96. "                    -noNRV......Ignore all non-record variant (NRV) data",
  97. "                    -NRVtable...Put NRV data values in the skeleton table",
  98. "                    -NRVfile....Put NRV data values in a separate file.  The",
  99. "                                name of this file will be <skeleton-path>.nrv",
  100. "                                if the -skeleton qualifier was used.  It will",
  101. "                                be <cdf-name>.nrv in the current directory",
  102. "                                (where <cdf-name> is the name portion of",
  103. "                                <cdf-path>) if -skeleton was not used.",
  104. #endif
  105. "",
  106. #if defined(vms)
  107. "               /[no]LOG",
  108. #endif
  109. #if defined(unix) | defined(__MSDOS__)
  110. "               -[no]log",
  111. #endif
  112. "                  Specifies whether or not messages are displayed as the",
  113. "                  program executes.  This includes both informatory and error",
  114. "                  messages.  The default is no logging.",
  115. "",
  116. #if defined(vms)
  117. "Example(s):    $ SKELETONTABLE/NOLOG FGGE3B",
  118. "               $ SKELETONTABLE/SKELETON=FGGE3BX FGGE3B",
  119. "               $ SKELETONTABLE/SKELETON=FGGE3BX/noNRV FGGE3B",
  120. #endif
  121. #if defined(unix)
  122. "Example(s):    % skeletontable -nolog fgge3b",
  123. "               % skeletontable -skeleton fgge3bx ../fgge3b",
  124. "               % skeletontable -skeleton ~user/cdf/fgge3b -noNRV fgge3b",
  125. #endif
  126. #if defined(__MSDOS__)
  127. "Example(s):    > skeletontable -nolog fgge3b",
  128. "               > skeletontable -skeleton fgge3bx a:\\fgge3b",
  129. "               > skeletontable -skeleton ..\\fgge3b -noNRV fgge3b",
  130. #endif
  131. NULL };
  132.  
  133. /******************************************************************************
  134. * main (cdf2skt).
  135. ******************************************************************************/
  136.  
  137. #if defined(vms)
  138. main (argc, argv)
  139. #else
  140. void main (argc, argv)
  141. #endif
  142. int argc;
  143. char *argv[];
  144. {
  145. CDFstatus status;
  146.  
  147. char CDFpath[MAX_PATH_LEN+1];
  148. char CDFpathX[MAX_PATH_LEN+1];
  149. char CDFname[MAX_NAME_LEN+1];
  150. char dir[MAX_DIR_LEN+1];
  151. char SKTpath[MAX_PATH_LEN+1];
  152. char SKTpathX[MAX_PATH_LEN+1];
  153. char NRVpath[MAX_PATH_LEN+1];
  154. char NRVpathX[MAX_PATH_LEN+1];
  155. char creationStamp[80+1];
  156.  
  157. int count;
  158. QOP *qop;
  159. static char *validQuals[] = { "skeleton", "noNRV", "NRVfile", "NRVtable",
  160.                   "log", "nolog", NULL };
  161. static int optRequired[] = { TRUE, FALSE, FALSE, FALSE,
  162.                  FALSE, FALSE, 0 };
  163.  
  164. /******************************************************************************
  165. * Parse qualifiers/options/parameters (QOP).
  166. ******************************************************************************/
  167.  
  168. switch (argc) {
  169.   case 1:
  170.     PageInst (instructions);
  171.     Exit;
  172.  
  173.   default:
  174.     qop = Qop (argc, argv, validQuals, optRequired);
  175.     if (qop == NULL) ExitBAD;
  176.  
  177.     /**************************************************************************
  178.     * Get CDF pathname.
  179.     **************************************************************************/
  180.  
  181.     if (qop->Nparms < 1) {
  182.       printf ("Missing parameter.\n");
  183.       ExitBAD;
  184.     }
  185.     else {
  186.       strcpy (CDFpath, qop->parms[0]);
  187.       ParsePath (CDFpath, dir, CDFname);
  188.     }
  189.  
  190.     /**************************************************************************
  191.     * Check if a skeleton table pathname was specified.
  192.     **************************************************************************/
  193.  
  194.     if (qop->qualEntered[0])
  195.       strcpy (SKTpath, qop->qualOpt[0]);
  196.     else
  197.       strcpy (SKTpath, CDFname);
  198.  
  199.     /**************************************************************************
  200.     * Check if the location of NRV data was specified.
  201.     **************************************************************************/
  202.  
  203.     count = 0;
  204.     if (qop->qualEntered[1]) count++;
  205.     if (qop->qualEntered[2]) count++;
  206.     if (qop->qualEntered[3]) count++;
  207.  
  208.     switch (count) {
  209.       case 0:
  210.     NRVloc = NRVinSKT;
  211.     break;
  212.  
  213.       case 1:
  214.     if (qop->qualEntered[1]) {
  215.       NRVloc = noNRV;
  216.       break;
  217.     }
  218.  
  219.     if (qop->qualEntered[2]) {
  220.       NRVloc = NRVinNRV;
  221.       strcpy (NRVpath, SKTpath);
  222.       break;
  223.     }
  224.  
  225.     if (qop->qualEntered[3]) {
  226.       NRVloc = NRVinSKT;
  227.       break;
  228.     }
  229.     break;
  230.  
  231.       case 2:
  232.       case 3:
  233.       default:
  234.     printf ("Specify only one NRV location\n");
  235.     ExitBAD;
  236.     }
  237.  
  238.     /**************************************************************************
  239.     * Check if message logging was specified.
  240.     **************************************************************************/
  241.  
  242.     count = 0;
  243.     if (qop->qualEntered[4]) count++;
  244.     if (qop->qualEntered[5]) count++;
  245.  
  246.     switch (count) {
  247.       case 0:
  248.     mLog = FALSE;
  249.     break;
  250.       case 1:
  251.     if (qop->qualEntered[4])
  252.       mLog = TRUE;
  253.     else
  254.       mLog = FALSE;
  255.     break;
  256.       default:
  257.     printf ("Conflicting qualifiers.\n");
  258.     ExitBAD;
  259.     }
  260. }
  261.   
  262. /*****************************************************************************/
  263.  
  264. if (mLog) printf ("\nName of CDF: %s\n", CDFname);
  265.  
  266. /******************************************************************************
  267. * Open skeleton table.
  268. ******************************************************************************/
  269.  
  270. strcat (SKTpath,".skt");
  271. if (mLog) printf ("\nName of Skeleton Table: %s\n", SKTpath);
  272.  
  273. strcpy (creationStamp, dateStamp());
  274.  
  275. ExpandPath (SKTpath, SKTpathX);
  276. SKTfp = fopen (SKTpathX, "w");
  277. if (SKTfp == NULL) {
  278.   printf ("Error creating skeleton table (%s).\n", SKTpath);
  279.   ExitBAD;
  280. }
  281.  
  282. fprintf (SKTfp, "! Skeleton table for the \"%s\" CDF.", CDFname);
  283. fprintf (SKTfp, "\n! Generated: %s", creationStamp);
  284.  
  285. /******************************************************************************
  286. * Open NRV file (if requested).
  287. ******************************************************************************/
  288.  
  289. switch (NRVloc) {
  290.   case NRVinNRV:
  291.     strcat (NRVpath, ".nrv");
  292.     if (mLog) printf ("\nNRV Data in: %s\n", NRVpath);
  293.     ExpandPath (NRVpath, NRVpathX);
  294.     NRVfp = fopen (NRVpathX, "w");
  295.     if (NRVfp == NULL) {
  296.       printf ("Error creating NRV file (%s).\n", NRVpath);
  297.       ExitBAD;
  298.     }
  299.     fprintf (NRVfp, "! NRV file for the \"%s\" CDF.", CDFname);
  300.     fprintf (NRVfp, "\n! Generated: %s", creationStamp);
  301.     break;
  302.   case NRVinSKT:
  303.     if (mLog) printf ("\nNRV Data in: %s\n", SKTpath);
  304.     break;
  305.   case noNRV:
  306.     if (mLog) printf ("\nNRV Data is ignored.\n");
  307.     break;
  308. }
  309.  
  310. /******************************************************************************
  311. * Open CDF.
  312. ******************************************************************************/
  313.  
  314. ExpandPath (CDFpath, CDFpathX);
  315. status = CDFlib (OPEN_, CDF_, CDFpathX, &id,
  316.          NULL_);
  317. StatusHandler (status);
  318.  
  319. /******************************************************************************
  320. * Write header section.
  321. ******************************************************************************/
  322.  
  323. if (mLog) printf ("\nPrinting Header Information...\n");
  324. WriteHeader (CDFname);
  325.  
  326. /******************************************************************************
  327. * Write global scope attributes section.
  328. ******************************************************************************/
  329.  
  330. if (mLog) printf ("\nPrinting Attribute Information...\n");
  331. WriteGlobalAttr ();
  332.  
  333. /******************************************************************************
  334. * Write variable scope attributes section.
  335. ******************************************************************************/
  336.  
  337. WriteVarAttr ();
  338.  
  339. /******************************************************************************
  340. * Write variables section.
  341. ******************************************************************************/
  342.  
  343. if (mLog) printf ("\nPrinting Variable Information...\n\n");
  344. WriteVar (NRVloc);
  345.  
  346. /******************************************************************************
  347. * End section.
  348. ******************************************************************************/
  349.  
  350. WriteEnd ();
  351.  
  352. /******************************************************************************
  353. * Clean up.
  354. ******************************************************************************/
  355.  
  356. status = CDFlib (SELECT_, CDF_, id,
  357.          CLOSE_, CDF_,
  358.          NULL_);
  359. StatusHandler (status);
  360.  
  361. fprintf (SKTfp, "\n");
  362. fclose (SKTfp);
  363.  
  364. if (NRVloc == NRVinNRV) {
  365.   fprintf (NRVfp, "\n");
  366.   fclose (NRVfp);
  367. }
  368.  
  369. Exit;
  370. }
  371.  
  372.  
  373. /******************************************************************************
  374. * WriteHeader.
  375. ******************************************************************************/
  376.  
  377. void WriteHeader (CDFname)
  378. char *CDFname;
  379. {
  380. CDFstatus status;
  381. long encoding, majority, format;
  382. long numDims;
  383. long dimSizes[CDF_MAX_DIMS];
  384. long numVars, numAttrs, maxRec;
  385. long numVattrs = 0, numGattrs = 0;
  386. long attrN;
  387. long scope;
  388. int dimN;
  389. char sizes[80+1];
  390. int Nblanks, Ndashes;
  391.  
  392. fprintf (SKTfp, "\n");
  393. fprintf (SKTfp, "\n#header");
  394.  
  395. status = CDFlib (SELECT_, CDF_, id,
  396.          GET_, CDF_ENCODING_, &encoding,
  397.                CDF_MAJORITY_, &majority,
  398.                CDF_FORMAT_, &format,
  399.                CDF_NUMDIMS_, &numDims,
  400.                CDF_DIMSIZES_, dimSizes,
  401.                CDF_NUMVARS_, &numVars,
  402.                CDF_NUMATTRS_, &numAttrs,
  403.                CDF_MAXREC_, &maxRec,
  404.          NULL_);
  405. StatusHandler (status);
  406.  
  407. for (attrN = 0; attrN < numAttrs; attrN++) {
  408.    status = CDFlib (SELECT_, CDF_, id,
  409.                  ATTR_, attrN,
  410.             GET_, ATTR_SCOPE_, &scope,
  411.             NULL_);
  412.    StatusHandler (status);
  413.  
  414.    switch (scope) {
  415.      case GLOBAL_SCOPE:
  416.      case GLOBAL_SCOPE_ASSUMED:
  417.        numGattrs++;
  418.        break;
  419.      case VARIABLE_SCOPE:
  420.      case VARIABLE_SCOPE_ASSUMED:
  421.        numVattrs++;
  422.        break;
  423.    }
  424. }
  425.  
  426. fprintf (SKTfp, "\n");
  427. fprintf (SKTfp, "\n                  CDF NAME: %s", CDFname);
  428.  
  429. fprintf (SKTfp, "\n             DATA ENCODING: %s", EncodingToken(encoding));
  430. fprintf (SKTfp, "\n                  MAJORITY: %s", MajorityToken(majority));
  431. fprintf (SKTfp, "\n                    FORMAT: %s", FormatToken(format));
  432.  
  433. if (numDims > 0) {
  434.   sizes[0] = NUL;
  435.   for (dimN = 0; dimN < numDims; dimN++) sprintf (&sizes[strlen(sizes)],
  436.                           "%ld ", dimSizes[dimN]);
  437.   sizes[strlen(sizes)-1] = NUL;            /* Wipe out trailing blank. */
  438. }
  439. else
  440.   strcpy (sizes, "! n/a");
  441.  
  442. fprintf (SKTfp, "\n");
  443. fprintf (SKTfp,
  444.      "\n! Variables G.Attributes V.Attributes Records  Dims  ");
  445.  
  446. if (strlen(sizes) > 5) {
  447.   Nblanks = (strlen(sizes)-5) / 2;
  448.   nCHARACTERS (SKTfp, Nblanks, ' ');
  449. }
  450. fprintf (SKTfp, "Sizes");
  451.  
  452. fprintf (SKTfp,
  453.      "\n! --------- ------------ ------------ -------  ----  ");
  454.  
  455. Ndashes = strlen(sizes) > 5 ? strlen(sizes) : 5;
  456. nCHARACTERS (SKTfp, Ndashes, '-');
  457.  
  458. fprintf (SKTfp, "\n%7ld %11ld %12ld %9ld  %6ld   ", numVars, numGattrs,
  459.                             numVattrs, maxRec + 1,
  460.                             numDims);
  461.  
  462. if (strlen(sizes) < 5) {
  463.   Nblanks = (5-strlen(sizes)) / 2;
  464.   nCHARACTERS (SKTfp, Nblanks, ' ');
  465. }
  466. fprintf (SKTfp, "%s", sizes);
  467.  
  468. return;
  469. }
  470.  
  471.  
  472. /******************************************************************************
  473. * WriteGlobalAttr.
  474. ******************************************************************************/
  475.  
  476. void WriteGlobalAttr ()
  477. {
  478. CDFstatus status;
  479. char delim;                 /* Delimeter for attribute name. */
  480. long attrN, numAttrs, scope, maxEntry;
  481. long dataType, numElements, entryN;
  482. int ccc;                 /* Current Cursor Column (base is 0). */
  483. void *value;
  484. char attrName[CDF_ATTR_NAME_LEN+1];
  485. int GattrCount = 0;             /* Number of global scope attributes. */
  486. int entryCount;
  487.  
  488. fprintf (SKTfp, "\n");
  489. fprintf (SKTfp, "\n");
  490. fprintf (SKTfp, "\n#GLOBALattributes");
  491.  
  492. status = CDFlib (SELECT_, CDF_, id,
  493.          GET_, CDF_NUMATTRS_, &numAttrs,
  494.          NULL_);
  495. StatusHandler (status);
  496.  
  497. if (numAttrs > 0) {
  498.   for (attrN = 0; attrN < numAttrs; attrN++) {
  499.      status = CDFlib (SELECT_, CDF_, id,
  500.                    ATTR_, attrN,
  501.               GET_, ATTR_NAME_, attrName,
  502.                 ATTR_SCOPE_, &scope,
  503.                 ATTR_MAXENTRY_, &maxEntry,
  504.               NULL_);
  505.      StatusHandler (status);
  506.      if (scope == GLOBAL_SCOPE || scope == GLOBAL_SCOPE_ASSUMED) {
  507.        /***********************************************************************
  508.        * Global scope attribute...
  509.        ***********************************************************************/
  510.  
  511.        GattrCount++;
  512.  
  513.        /***********************************************************************
  514.        * If first global scope attribute found, print field headings.
  515.        ***********************************************************************/
  516.  
  517.        if (GattrCount == 1) {
  518.      fprintf (SKTfp, "\n");
  519.      fprintf (SKTfp, "\n");
  520.      ccc = fprintf (SKTfp, "! Attribute");
  521.      nCHARACTERS (SKTfp, ENTRY_NUM_COL - ccc, ' ');
  522.      ccc += ENTRY_NUM_COL - ccc;
  523.      ccc += fprintf (SKTfp, "Entry       Data");
  524.  
  525.      fprintf (SKTfp, "\n");
  526.      ccc = fprintf (SKTfp, "! Name     ");
  527.      nCHARACTERS (SKTfp, ENTRY_NUM_COL - ccc, ' ');
  528.      ccc += ENTRY_NUM_COL - ccc;
  529.      ccc += fprintf (SKTfp, "Number      Type       Value");
  530.  
  531.      fprintf (SKTfp, "\n");
  532.      ccc = fprintf (SKTfp, "! ---------");
  533.      nCHARACTERS (SKTfp, ENTRY_NUM_COL - ccc, ' ');
  534.      ccc += ENTRY_NUM_COL - ccc;
  535.      ccc += fprintf (SKTfp, "------      ----       -----");
  536.        }
  537.  
  538.        /***********************************************************************
  539.        * Write name.
  540.        ***********************************************************************/
  541.  
  542.        delim = PickDelim (attrName);
  543.        fprintf (SKTfp, "\n");
  544.        fprintf (SKTfp, "\n");
  545.        ccc = fprintf (SKTfp, "  %c%s%c", delim, attrName, delim);
  546.  
  547.        /***********************************************************************
  548.        * Write each entry.
  549.        ***********************************************************************/
  550.  
  551.        entryCount = 0;
  552.  
  553.        for (entryN = 0; entryN <= maxEntry; entryN++) {
  554.       status = CDFlib (SELECT_, CDF_, id,
  555.                     ENTRY_, entryN,
  556.                GET_, ENTRY_DATATYPE_, &dataType,
  557.                  ENTRY_NUMELEMS_, &numElements,
  558.                NULL_);
  559.       if (status != NO_SUCH_ENTRY) {
  560.         StatusHandler (status);
  561.         entryCount++;
  562.  
  563.         MALLOC (value, ElemSize(dataType) * numElements);
  564.  
  565.         status = CDFlib (SELECT_, CDF_, id,
  566.                  GET_, ENTRY_DATA_, value,
  567.                  NULL_);
  568.         StatusHandler (status);
  569.  
  570.         if (entryCount == 1) {
  571.           if (ccc > ENTRY_NUM_COL - 1) {
  572.         fprintf (SKTfp, "\n");
  573.         ccc = 0;
  574.           }
  575.         }
  576.         else {
  577.           fprintf (SKTfp, "\n");
  578.           ccc = 0;
  579.         }
  580.  
  581.         nCHARACTERS (SKTfp, ENTRY_NUM_COL - ccc, ' ');
  582.         ccc += ENTRY_NUM_COL - ccc;
  583.  
  584.         ccc += fprintf (SKTfp, "%*ld:    %s   ",
  585.                 ENTRY_NUM_WIDTH, entryN + 1, DataType(dataType));
  586.  
  587.         ccc += fprintf (SKTfp, "{ ");
  588.         WriteEntryValue (SKTfp, dataType, numElements,
  589.                  value, ccc,
  590.                  MAX_COL_TO_USE - 4);      /* -4 for possible " } ." */
  591.         ccc += fprintf (SKTfp, " }");
  592.  
  593.         free (value);        
  594.       }
  595.        }
  596.        /***********************************************************************
  597.        * Write '.' after last entry.
  598.        ***********************************************************************/
  599.  
  600.        fprintf (SKTfp, " .");
  601.      }
  602.   }
  603.   if (GattrCount == 0) fprintf (SKTfp, "\n\n! No global scope attributes.");
  604. }
  605. else
  606.   fprintf (SKTfp, "\n\n! No global scope attributes.");
  607.  
  608. return;
  609. }
  610.  
  611.  
  612. /******************************************************************************
  613. * WriteVarAttr.
  614. ******************************************************************************/
  615.  
  616. void WriteVarAttr ()
  617. {
  618. CDFstatus status;
  619. char delim;                 /* Delimeter for attribute name. */
  620. long attrN, numAttrs, scope;
  621. char attrName[CDF_ATTR_NAME_LEN+1];
  622. int VattrCount = 0;             /* Number of variable scope attributes. */
  623.  
  624. fprintf (SKTfp, "\n");
  625. fprintf (SKTfp, "\n");
  626. fprintf (SKTfp, "\n#VARIABLEattributes");
  627. fprintf (SKTfp, "\n");
  628.  
  629. status = CDFlib (SELECT_, CDF_, id,
  630.          GET_, CDF_NUMATTRS_, &numAttrs,
  631.          NULL_);
  632. StatusHandler (status);
  633.  
  634. if (numAttrs > 0) {
  635.   for (attrN = 0; attrN < numAttrs; attrN++) {
  636.      status = CDFlib (SELECT_, CDF_, id,
  637.                    ATTR_, attrN,
  638.               GET_, ATTR_NAME_, attrName,
  639.                 ATTR_SCOPE_, &scope,
  640.               NULL_);
  641.      StatusHandler (status);
  642.      if (scope == VARIABLE_SCOPE || scope == VARIABLE_SCOPE_ASSUMED) {
  643.        /***********************************************************************
  644.        * Variable scope attribute...
  645.        ***********************************************************************/
  646.        VattrCount++;
  647.        delim = PickDelim (attrName);
  648.        fprintf (SKTfp, "\n  %c%s%c", delim, attrName, delim);
  649.      }
  650.   }
  651.   if (VattrCount == 0) fprintf (SKTfp, "\n! No variable scope attributes.");
  652. }
  653. else
  654.   fprintf (SKTfp, "\n! No variable scope attributes.");
  655.  
  656. return;
  657. }
  658.  
  659.  
  660. /******************************************************************************
  661. * WriteVar.
  662. ******************************************************************************/
  663.  
  664. void WriteVar (NRVloc)
  665. enum NRVlocENUM NRVloc;
  666. {
  667. CDFstatus status;
  668. long numDims, numVars, numAttrs;
  669. long maxRec;
  670. long varDataType, varNumElements;
  671. long entryDataType, entryNumElements;
  672. long recVary, dimVarys[CDF_MAX_DIMS];
  673. char varName[CDF_VAR_NAME_LEN+1];
  674. char attrName[CDF_ATTR_NAME_LEN+1];
  675. char delim;
  676. long varN, attrN;
  677. long scope;
  678. int dimN;
  679. int ccc;                  /* Current Cursor Column (base is 0). */
  680. void *value;
  681. int VattrCount;
  682. char varys[80+1];
  683. int Nblanks, Ndashes;
  684.  
  685. fprintf (SKTfp, "\n");
  686. fprintf (SKTfp, "\n");
  687. fprintf (SKTfp, "\n#variables");
  688.  
  689. status = CDFlib (SELECT_, CDF_, id,
  690.          GET_, CDF_NUMDIMS_, &numDims,
  691.                CDF_NUMVARS_, &numVars,
  692.                CDF_NUMATTRS_, &numAttrs,
  693.                CDF_MAXREC_, &maxRec,
  694.          NULL_);
  695. StatusHandler (status);
  696.  
  697. /******************************************************************************
  698. * Are there any variables?...
  699. ******************************************************************************/
  700.  
  701. if (numVars == 0) {
  702.   fprintf (SKTfp, "\n\n! No variables.");
  703.   if (NRVloc == NRVinNRV) fprintf (NRVfp, "\n\n! No variables.");
  704.   return;
  705. }
  706.  
  707. /******************************************************************************
  708. * ...yes, print them.
  709. ******************************************************************************/
  710.  
  711. for (varN = 0; varN < numVars; varN++) {
  712.    /***************************************************************************
  713.    * Inquire variable.
  714.    ***************************************************************************/
  715.  
  716.    status = CDFlib (SELECT_, CDF_, id,
  717.                  VAR_, varN,
  718.             GET_, VAR_NAME_, varName,
  719.               VAR_DATATYPE_, &varDataType,
  720.               VAR_NUMELEMS_, &varNumElements,
  721.               VAR_RECVARY_, &recVary,
  722.               VAR_DIMVARYS_, dimVarys,
  723.             NULL_);
  724.    StatusHandler (status);
  725.  
  726.    /***************************************************************************
  727.    * Build dimension variance string.
  728.    ***************************************************************************/
  729.  
  730.    if (numDims > 0) {
  731.      varys[0] = NUL;
  732.      for (dimN = 0; dimN < numDims; dimN++) {
  733.     strcat (varys, TFvarianceToken(dimVarys[dimN]));
  734.     strcat (varys, " ");
  735.      }
  736.      varys[strlen(varys)-1] = NUL;        /* Wipe out trailing blank. */
  737.    }
  738.    else
  739.      strcpy (varys, "! n/a");
  740.  
  741.    /***************************************************************************
  742.    * Write headings.
  743.    ***************************************************************************/
  744.  
  745.    if (varN != 0) fprintf (SKTfp, "\n");    /* Skip 2 lines after first
  746.                            variables. */
  747.    fprintf (SKTfp, "\n");
  748.    fprintf (SKTfp, "\n");
  749.    ccc = fprintf (SKTfp, "! Variable");
  750.    nCHARACTERS (SKTfp, VAR_DATATYPE_COL - ccc, ' ');
  751.    ccc += VAR_DATATYPE_COL - ccc;
  752.    ccc += fprintf (SKTfp, "  Data       Number     Record    ");
  753.  
  754.    if (strlen(varys) > 9) {
  755.      Nblanks = (strlen(varys)-9) / 2;
  756.      nCHARACTERS (SKTfp, Nblanks, ' ');
  757.    }
  758.    fprintf (SKTfp, "Dimension");
  759.  
  760.    fprintf (SKTfp, "\n");
  761.    ccc = fprintf (SKTfp, "! Name     ");
  762.    nCHARACTERS (SKTfp, VAR_DATATYPE_COL - ccc, ' ');
  763.    ccc += VAR_DATATYPE_COL - ccc;
  764.    ccc += fprintf (SKTfp, "  Type      Elements   Variance   ");
  765.  
  766.    if (strlen(varys) > 9) {
  767.      Nblanks = (strlen(varys)-9) / 2;
  768.      nCHARACTERS (SKTfp, Nblanks, ' ');
  769.    }
  770.    fprintf (SKTfp, "Variances");
  771.  
  772.    fprintf (SKTfp, "\n");
  773.    ccc = fprintf (SKTfp, "! --------");
  774.    nCHARACTERS (SKTfp, VAR_DATATYPE_COL - ccc, ' ');
  775.    ccc += VAR_DATATYPE_COL - ccc;
  776.    ccc += fprintf (SKTfp, "  ----      --------   --------   ");
  777.  
  778.    Ndashes = strlen(varys) > 9 ? strlen(varys) : 9;
  779.    nCHARACTERS (SKTfp, Ndashes, '-');
  780.  
  781.    /***************************************************************************
  782.    * Write variable definition line.
  783.    ***************************************************************************/
  784.  
  785.    fprintf (SKTfp, "\n");
  786.  
  787.    delim = PickDelim (varName);
  788.    fprintf (SKTfp, "\n");
  789.    ccc = fprintf (SKTfp, "  %c%s%c", delim, varName, delim);
  790.  
  791.    if (ccc > VAR_DATATYPE_COL - 3) {
  792.      fprintf (SKTfp, "\n");
  793.      ccc = 0;
  794.    }
  795.  
  796.    nCHARACTERS (SKTfp, VAR_DATATYPE_COL - ccc, ' ');
  797.    ccc += VAR_DATATYPE_COL - ccc;
  798.  
  799.    ccc += fprintf (SKTfp, "%s  %*ld          %c      ",
  800.            DataType(varDataType), VAR_NUMELEMS_WIDTH, varNumElements,
  801.            (recVary ? 'T' : 'F'));
  802.  
  803.    if (strlen(varys) < 9) {
  804.      Nblanks = (9-strlen(varys)) / 2;
  805.      nCHARACTERS (SKTfp, Nblanks, ' ');
  806.    }
  807.    fprintf (SKTfp, "%s", varys);
  808.  
  809.    /***************************************************************************
  810.    * Write corresponding attribute entries.
  811.    ***************************************************************************/
  812.  
  813.    VattrCount = 0;
  814.  
  815.    fprintf (SKTfp, "\n");
  816.  
  817.    for (attrN = 0; attrN < numAttrs; attrN++) {
  818.       status = CDFlib (SELECT_, CDF_, id,
  819.                 ATTR_, attrN,
  820.                GET_, ATTR_SCOPE_, &scope,
  821.                  ATTR_NAME_, attrName,
  822.                NULL_);
  823.       StatusHandler (status);
  824.  
  825.       if (scope == VARIABLE_SCOPE || scope == VARIABLE_SCOPE_ASSUMED) {
  826.     status = CDFlib (SELECT_, CDF_, id,
  827.                   ENTRY_, varN,
  828.              GET_, ENTRY_DATATYPE_, &entryDataType,
  829.                    ENTRY_NUMELEMS_, &entryNumElements,
  830.              NULL_);
  831.     if (status != NO_SUCH_ENTRY) {
  832.       StatusHandler (status);
  833.       VattrCount++;
  834.  
  835.       if (VattrCount == 1) {
  836.         fprintf (SKTfp, "\n");
  837.         ccc = fprintf (SKTfp, "  ! Attribute");
  838.         nCHARACTERS (SKTfp, ENTRY_DATATYPE_COL - ccc, ' ');
  839.         ccc += ENTRY_DATATYPE_COL - ccc;
  840.         ccc += fprintf (SKTfp, "  Data");
  841.  
  842.         fprintf (SKTfp, "\n");
  843.         ccc = fprintf (SKTfp, "  ! Name     ");
  844.         nCHARACTERS (SKTfp, ENTRY_DATATYPE_COL - ccc, ' ');
  845.         ccc += ENTRY_DATATYPE_COL - ccc;
  846.         ccc += fprintf (SKTfp, "  Type       Value");
  847.  
  848.         fprintf (SKTfp, "\n");
  849.         ccc = fprintf (SKTfp, "  ! --------");
  850.         nCHARACTERS (SKTfp, ENTRY_DATATYPE_COL - ccc, ' ');
  851.         ccc += ENTRY_DATATYPE_COL - ccc;
  852.         ccc += fprintf (SKTfp, "  ----       -----");
  853.  
  854.         fprintf (SKTfp, "\n");
  855.       }
  856.  
  857.       MALLOC (value, entryNumElements * ElemSize(entryDataType));
  858.  
  859.       status = CDFlib (SELECT_, CDF_, id,
  860.                GET_, ENTRY_DATA_, value,
  861.                NULL_);
  862.       StatusHandler (status);
  863.  
  864.       delim = PickDelim (attrName);
  865.       fprintf (SKTfp, "\n");
  866.       ccc = fprintf (SKTfp, "    %c%s%c", delim, attrName, delim);
  867.  
  868.       if (ccc > ENTRY_DATATYPE_COL - 3) {
  869.         fprintf (SKTfp, "\n");
  870.         ccc = 0;
  871.       }
  872.  
  873.       nCHARACTERS (SKTfp, ENTRY_DATATYPE_COL - ccc, ' ');
  874.       ccc += ENTRY_DATATYPE_COL - ccc;
  875.  
  876.       ccc += fprintf (SKTfp, "%s   ", DataType(entryDataType));
  877.  
  878.       ccc += fprintf (SKTfp, "{ ");
  879.       WriteEntryValue (SKTfp, entryDataType, entryNumElements,
  880.                value, ccc,
  881.                MAX_COL_TO_USE - 4);      /* -4 for possible " } ." */
  882.       ccc += fprintf (SKTfp, " }");
  883.  
  884.       free (value);
  885.     }
  886.       }
  887.    }
  888.  
  889.    if (VattrCount == 0) {
  890.      fprintf (SKTfp,
  891.           "\n  ! No variable scope attribute entries for this variable.");
  892.    }
  893.  
  894.    /***************************************************************************
  895.    * Write terminating period.
  896.    ***************************************************************************/
  897.  
  898.    if (VattrCount == 0) {
  899.      fprintf (SKTfp, "\n\n  .");
  900.      fprintf (SKTfp,
  901.           "                         ! Terminating period required.");
  902.    }
  903.    else
  904.      fprintf (SKTfp, " .");
  905.  
  906.    /***************************************************************************
  907.    * Write NRV data (if requested).
  908.    ***************************************************************************/
  909.  
  910.    if (! recVary)
  911.      switch (NRVloc) {
  912.        case noNRV:
  913.      fprintf (SKTfp, "\n");
  914.      fprintf (SKTfp, "\n  ! NRV values were not requested.");
  915.      break;
  916.  
  917.        case NRVinSKT:
  918.      fprintf (SKTfp, "\n");
  919.      if (maxRec > -1) {
  920.        fprintf (SKTfp, "\n  ! NRV values follow...");
  921.        fprintf (SKTfp, "\n");
  922.            WriteNRVdata (SKTfp, varN, varDataType, varNumElements, dimVarys);
  923.      }
  924.      else
  925.        fprintf (SKTfp,"\n  ! No NRV values (no records in CDF).");
  926.          break;
  927.  
  928.        case NRVinNRV:
  929.      fprintf (SKTfp, "\n");
  930.      fprintf (SKTfp, "\n  ! NRV values in NRV file.");
  931.  
  932.      if (maxRec > -1) {
  933.        delim = PickDelim (varName);
  934.        fprintf (NRVfp, "\n");
  935.            fprintf (NRVfp, "\n%c%s%c", delim, varName, delim);
  936.        fprintf (NRVfp, "\n");
  937.        WriteNRVdata (NRVfp, varN, varDataType, varNumElements, dimVarys);
  938.      }
  939.      else {            /* Only one comment (on first variable). */
  940.        if (varN == 0)
  941.          fprintf (NRVfp, "\n\n! No NRV values (no records in CDF).");
  942.      }
  943.  
  944.          break;
  945.      }
  946. }
  947.  
  948. return;
  949. }
  950.  
  951.  
  952. /******************************************************************************
  953. * WriteEnd.
  954. ******************************************************************************/
  955.  
  956. void WriteEnd ()
  957. {
  958. fprintf (SKTfp, "\n");
  959. fprintf (SKTfp, "\n");
  960. fprintf (SKTfp, "\n#end");
  961.  
  962. return;
  963. }
  964.  
  965.  
  966. /******************************************************************************
  967. * WriteNRVdata.  It is assumed that there is at least one record in the CDF.
  968. ******************************************************************************/
  969.  
  970. void WriteNRVdata (fp, varN, dataType, numElements, dimVarys)
  971. FILE *fp;
  972. long varN;
  973. long dataType;
  974. long numElements;
  975. long dimVarys[];
  976. {
  977. CDFstatus status;
  978. long indices[CDF_MAX_DIMS];
  979. long counts[CDF_MAX_DIMS];
  980. void *value;
  981. long numDims, dimSizes[CDF_MAX_DIMS];
  982. long majority;
  983. int ccc;
  984. long nValues, i;
  985. int dimN;
  986. char Evalue[80+1];
  987.  
  988. status = CDFlib (SELECT_, CDF_, id,
  989.          GET_, CDF_NUMDIMS_, &numDims,
  990.                CDF_DIMSIZES_, dimSizes,
  991.                CDF_MAJORITY_, &majority,
  992.          NULL_);
  993. StatusHandler (status);
  994.  
  995. nValues = 1;
  996. if (numDims > 0)
  997.   for (dimN = 0; dimN < numDims; dimN++) {
  998.      indices[dimN] = 0;
  999.      if (dimVarys[dimN]) {
  1000.        nValues *= dimSizes[dimN];
  1001.        counts[dimN] = dimSizes[dimN];
  1002.      }
  1003.      else
  1004.        counts[dimN] = 1;
  1005.   }
  1006.  
  1007. MALLOC (value, numElements * ElemSize(dataType));
  1008.  
  1009. status = CDFlib (SELECT_, CDF_, id,
  1010.               VAR_, varN,
  1011.               CDF_RECNUMBER_, (long) 0,
  1012.          NULL_);
  1013. StatusHandler (status);
  1014.  
  1015. for (i = 0; i < nValues; i++) {
  1016.    status = CDFlib (SELECT_, CDF_, id,
  1017.                  CDF_DIMINDICES_, indices,
  1018.             GET_, VAR_DATA_, value,
  1019.             NULL_);
  1020.    StatusHandler (status);
  1021.  
  1022.    fprintf (fp, "\n");
  1023.    ccc = fprintf (fp, "  [");
  1024.  
  1025.    if (numDims > 0)
  1026.       for (dimN = 0; dimN < numDims; dimN++) {
  1027.          ccc += fprintf (fp, " %ld", indices[dimN] + 1);
  1028.      if (dimN != numDims - 1) ccc += fprintf (fp, ",");
  1029.       }
  1030.  
  1031.    ccc += fprintf (fp, " ] = ");
  1032.  
  1033.    switch (dataType) {
  1034.      case CDF_CHAR:
  1035.      case CDF_UCHAR:
  1036.        ccc += fprintf (fp, "{ ");
  1037.        WriteStringValue (fp, numElements, value,
  1038.              ccc, MAX_COL_TO_USE + 2);    /* +2 for " }" */
  1039.        fprintf (fp, " }");                /* Don't care about
  1040.                                'ccc' anymore. */
  1041.        break;
  1042.      default:
  1043.        EncodeValue (dataType, value, Evalue);    /* Don't care about width. */
  1044.        fprintf (fp, "%s", Evalue);
  1045.        break;
  1046.    }
  1047.  
  1048.    if (majority == ROW_MAJOR) {
  1049.      INCRindicesROW (numDims, counts, indices);
  1050.    }
  1051.    else {
  1052.      INCRindicesCOL (numDims, counts, indices);
  1053.    }
  1054. }
  1055.  
  1056. return;
  1057. }
  1058.  
  1059. /******************************************************************************
  1060. * DataType.  Return address of character string for data type.
  1061. ******************************************************************************/
  1062.  
  1063. char *DataType(dataType)
  1064. long dataType;
  1065. {
  1066. switch (dataType) {
  1067.   case CDF_BYTE:   return "CDF_BYTE  ";
  1068.   case CDF_INT1:   return "CDF_INT1  ";
  1069.   case CDF_INT2:   return "CDF_INT2  ";
  1070.   case CDF_INT4:   return "CDF_INT4  ";
  1071.   case CDF_UINT1:  return "CDF_UINT1 ";
  1072.   case CDF_UINT2:  return "CDF_UINT2 ";
  1073.   case CDF_UINT4:  return "CDF_UINT4 ";
  1074.   case CDF_REAL4:  return "CDF_REAL4 ";
  1075.   case CDF_REAL8:  return "CDF_REAL8 ";
  1076.   case CDF_FLOAT:  return "CDF_FLOAT ";
  1077.   case CDF_DOUBLE: return "CDF_DOUBLE";
  1078.   case CDF_EPOCH:  return "CDF_EPOCH ";
  1079.   case CDF_CHAR:   return "CDF_CHAR  ";
  1080.   case CDF_UCHAR:  return "CDF_UCHAR ";
  1081. }
  1082. return "??????????";
  1083. }
  1084.  
  1085. /******************************************************************************
  1086. * StatusHandler.
  1087. ******************************************************************************/
  1088.  
  1089. void StatusHandler (status)
  1090. CDFstatus status;
  1091. {
  1092. char text[CDF_STATUSTEXT_LEN + 1];
  1093.  
  1094. if (status == CDF_OK) return;        /* Do nothing. */
  1095.  
  1096. CDFlib (SELECT_, CDF_STATUS_, status,
  1097.     GET_, STATUS_TEXT_, text,
  1098.     NULL_);
  1099.  
  1100. if (status < CDF_WARN) {
  1101.   printf ("\nERROR> %s\n", text);
  1102.   CDFlib (SELECT_, CDF_, id,
  1103.       CLOSE_, CDF_,
  1104.       NULL_);
  1105.  
  1106.   printf ("**** THE SKELETON TABLE IS INCOMPLETE ****\n");
  1107.   fprintf (SKTfp, "\n");
  1108.   fprintf (SKTfp, "\n**** THIS SKELETON TABLE IS NOT COMPLETE ****");
  1109.   fprintf (SKTfp, "\n");
  1110.   fclose (SKTfp);
  1111.  
  1112.   if (NRVloc == NRVinNRV) {
  1113.     printf ("**** THE NRV FILE IS INCOMPLETE ****\n");
  1114.     fprintf (NRVfp, "\n");
  1115.     fprintf (NRVfp, "\n**** THIS NRV FILE IS NOT COMPLETE ****");
  1116.     fprintf (NRVfp, "\n");
  1117.     fclose (NRVfp);
  1118.   }
  1119.  
  1120.   ExitBAD;
  1121. }
  1122. else
  1123.   if (mLog)
  1124.     if (status < CDF_OK)
  1125.       printf ("WARNING> %s\n", text);
  1126.     else
  1127.       printf ("INFO> %s\n", text);
  1128.  
  1129. return;
  1130. }
  1131.